package cn.iocoder.yudao.module.reclaim.controller.admin.productcheckdeductitem;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.reclaim.controller.admin.productcheckdeductitem.vo.ReclaimCheckDeductItemPageReqVO;
import cn.iocoder.yudao.module.reclaim.controller.admin.productcheckdeductitem.vo.ReclaimCheckDeductItemRespVO;
import cn.iocoder.yudao.module.reclaim.controller.admin.productcheckdeductitem.vo.ReclaimCheckDeductItemSaveReqVO;
import cn.iocoder.yudao.module.reclaim.dal.dataobject.productcheckres.ReclaimCheckDeductItemDO;
import cn.iocoder.yudao.module.reclaim.service.productcheckdeductitem.ReclaimCheckDeductItemService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 回收核验扣除项")
@RestController
@RequestMapping("/reclaim/product-check-deduct-item")
@Validated
public class ReclaimCheckDeductItemController {

    @Resource
    private ReclaimCheckDeductItemService productCheckDeductItemService;

    @PostMapping("/create")
    @Operation(summary = "创建回收核验扣除项")
    @PreAuthorize("@ss.hasPermission('reclaim:product-check-deduct-item:create')")
    public CommonResult<Long> createProductCheckDeductItem(@Valid @RequestBody ReclaimCheckDeductItemSaveReqVO createReqVO) {
        return success(productCheckDeductItemService.createProductCheckDeductItem(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新回收核验扣除项")
    @PreAuthorize("@ss.hasPermission('reclaim:product-check-deduct-item:update')")
    public CommonResult<Boolean> updateProductCheckDeductItem(@Valid @RequestBody ReclaimCheckDeductItemSaveReqVO updateReqVO) {
        productCheckDeductItemService.updateProductCheckDeductItem(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除回收核验扣除项")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('reclaim:product-check-deduct-item:delete')")
    public CommonResult<Boolean> deleteProductCheckDeductItem(@RequestParam("id") Long id) {
        productCheckDeductItemService.deleteProductCheckDeductItem(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得回收核验扣除项")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('reclaim:product-check-deduct-item:query')")
    public CommonResult<ReclaimCheckDeductItemRespVO> getProductCheckDeductItem(@RequestParam("id") Long id) {
        ReclaimCheckDeductItemDO productCheckDeductItem = productCheckDeductItemService.getProductCheckDeductItem(id);
        return success(BeanUtils.toBean(productCheckDeductItem, ReclaimCheckDeductItemRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得回收核验扣除项分页")
    @PreAuthorize("@ss.hasPermission('reclaim:product-check-deduct-item:query')")
    public CommonResult<PageResult<ReclaimCheckDeductItemRespVO>> getProductCheckDeductItemPage(@Valid ReclaimCheckDeductItemPageReqVO pageReqVO) {
        PageResult<ReclaimCheckDeductItemDO> pageResult = productCheckDeductItemService.getProductCheckDeductItemPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ReclaimCheckDeductItemRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出回收核验扣除项 Excel")
    @PreAuthorize("@ss.hasPermission('reclaim:product-check-deduct-item:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportProductCheckDeductItemExcel(@Valid ReclaimCheckDeductItemPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ReclaimCheckDeductItemDO> list = productCheckDeductItemService.getProductCheckDeductItemPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "回收核验扣除项.xls", "数据", ReclaimCheckDeductItemRespVO.class,
                        BeanUtils.toBean(list, ReclaimCheckDeductItemRespVO.class));
    }

}